﻿using System.Linq;

using System;
using System.Collections.Generic;
using System.Web;
using System.Runtime.Serialization;
using System.Text;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Linq;

namespace LoginService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public const string koneksi = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Haris\Documents\KULIAH\Semester 6\Framework .NET (E)\FP_DOTNET_dev\LoginService\LoginService\App_Data\dotnet.mdf;Integrated Security=True;User Instance=True");
        public ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(koneksi).ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PegawaiMap>())
                .BuildSessionFactory();
        }

        public bool ceklogin(Employee emp)
        {
            var nhibernateHelper = CreateSessionFactory();
            Pegawai tempPegawai = new Pegawai();
            int jumItem = 0;

            using (var session = nhibernateHelper.OpenSession())
            {
                var pass = (from make in session.Query<Pegawai>()
                            where make.username == emp.Username_Pegawai
                            select make).ToList<Pegawai>();
                jumItem = pass.Count();
                if (jumItem > 0) tempPegawai = pass.First<Pegawai>();

            }

            if (tempPegawai == null)

                //command = "Failed tidak ditemukan";
                return false;
            else
            {

                if (emp.Username_Pegawai == tempPegawai.username && emp.Password_Pegawai == tempPegawai.password)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        //public bool ceklogin(Employee emp)
        //{
        //    throw new System.NotImplementedException();
        //}
    }
}
